﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>
<%@ Import Namespace=" System.Collections.Generic" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="Data" %>
<script RunAt="server">


    void Application_Start(object sender, EventArgs e)
    {
        System.Timers.Timer objTimer = new System.Timers.Timer();
        objTimer.Interval = 1000 * 1 * 1; //这个时间单位毫秒,比如10秒，就写10000 86400000
        objTimer.Enabled = true;
        objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);


        System.Timers.Timer objTimer6 = new System.Timers.Timer();
        objTimer6.Interval = 1000 * 1 * 1;
        objTimer6.Enabled = true;
        objTimer6.Elapsed += new ElapsedEventHandler(UpdateFangAnState);//开奖前更新方案状态

        System.Timers.Timer objTimer2 = new System.Timers.Timer();
        objTimer2.Interval = 1000 * 1 * 1;//1分钟
        objTimer2.Enabled = true;
        objTimer2.Elapsed += new ElapsedEventHandler(ComputeMediaForecaststatistics);//专业媒体预测计算更新

        System.Timers.Timer objTimer3 = new System.Timers.Timer();
        objTimer3.Interval = 1000 * 1 * 1;//1分钟
        objTimer3.Enabled = true;
        objTimer3.Elapsed += new ElapsedEventHandler(GetPageInfo);//获取开奖数据

        System.Timers.Timer objTimer4 = new System.Timers.Timer();
        objTimer4.Interval = 1000 * 1 * 1;
        objTimer4.Enabled = true;
        objTimer4.Elapsed += new ElapsedEventHandler(ZigouSumitWin);//自购派奖

        System.Timers.Timer objTimer5 = new System.Timers.Timer();
        objTimer5.Interval = 1000 * 1 * 1;
        objTimer5.Enabled = true;
        objTimer5.Elapsed += new ElapsedEventHandler(HemaiSumitWin);//合买派奖



    }

    void UpdateFangAnState(object sender, ElapsedEventArgs e)
    {
        string strweek = e.SignalTime.DayOfWeek.ToString();
        int inthour = e.SignalTime.Hour;
        int intminute = e.SignalTime.Minute;
        int intSecond = e.SignalTime.Second;
        int ihour = 19;
        int iminute = 01;
        int iSecond = 00;
        if (inthour == ihour && intminute == iminute && intSecond == iSecond)//晚上7点执行
        {
            int totalCount = 0;
            List<Model.LotteryQs> lottery_list = BLL.LotteryQs.GetPageList_Where(0, int.MaxValue, 0, out totalCount);
            for (int i = 0; i < lottery_list.Count; i++)
            {
                //如果当天开奖，那么在开奖之前更新即将开奖的彩种的合买方案的状态
                if (lottery_list[i].pre_opentime.Date.ToString("yyyyMMdd") == DateTime.Now.Date.ToString("yyyyMMdd"))
                {
                    if (lottery_list[i].LotteryType == 1)//双色球
                    {
                        BLL.SubmitWin_hemai.UpdateLotteryChippedState(1, lottery_list[i].Lotteryqs);//更新合买方案的状态
                    }
                    else if (lottery_list[i].LotteryType == 2)
                    {
                        BLL.SubmitWin_hemai.UpdateLotteryChippedState(2, lottery_list[i].Lotteryqs);
                    }
                    else if (lottery_list[i].LotteryType == 5)
                    {
                        BLL.SubmitWin_hemai.UpdateLotteryChippedState(5, lottery_list[i].Lotteryqs);
                    }
                }

            }
        }

    }
    void ZigouSumitWin(object sender, ElapsedEventArgs e)
    {
        int inthour = e.SignalTime.Hour;
        int ihour = 05;
        //int intminute = e.SignalTime.Minute;
        //int iminute =01;
        //int intSecond = e.SignalTime.Second;
        //int iSecond = 00;
        int logcount = 0;
        if (inthour == ihour)//凌晨5点,自购派奖
        {
            BLL.SubmitWin_zigou.SubmitWin();
            logcount += 1;
            WLog(logcount);

        }
    }
    public void WLog(int log)
    {
        string da = DateTime.Now.ToString("yy-MM-dd");
        FileInfo file = new FileInfo(@"D:/wwwroot/cpmimi_894bf1/web/log/" + da + ".txt");//创建文件
        string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        StreamWriter sw = file.AppendText();//打开追加流

        sw.Write("" + date + "," + log + "");//追加数据

        sw.Dispose();//释放资源,关闭文件

    }
    void HemaiSumitWin(object sender, ElapsedEventArgs e)
    {
        int inthour = e.SignalTime.Hour;
        //int intminute = e.SignalTime.Minute;
        //int iminute = 01;
        int ihour = 04;
        int logcount = 0;
        //int intSecond = e.SignalTime.Second;
        //int iSecond = 00;
        if (inthour == ihour )//凌晨6点,自购派奖
        {
            BLL.SubmitWin_hemai.SubmitWin();
            logcount += 1;
            WLog(logcount);
        }
    }

    void objTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        string strweek = e.SignalTime.DayOfWeek.ToString();
        int inthour = e.SignalTime.Hour;
        int intminute = e.SignalTime.Minute;
        int intSecond = e.SignalTime.Second;
        int ihour = 00;
        int iminute = 01;
        int iSecond = 00;
        if (inthour == ihour && intminute == iminute && intSecond == iSecond)//半夜12点执行
        {
            #region 自动生成每种彩票的期数
            int totalCount = 0;
            //更新每种彩票的期数
            List<Model.LotteryQs> lotteryQs = BLL.LotteryQs.GetPageList_Where(0, int.MaxValue, 0, out totalCount);
            for (int i = 0; i < lotteryQs.Count; i++)
            {
                string qs = "";
                int week = selectNextDate(DateTime.Now);
                DateTime timer = new DateTime();
                DateTime pre_opentime = new DateTime();
                if (lotteryQs[i].LotteryType == 1)//2，4，7开奖
                {
                    if (week == 1 || week == 3 || week == 5)//双色球
                    {
                        qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                        timer = DateTime.Now;
                        if (week == 1 || week == 3)
                        {
                            pre_opentime = timer.AddDays(1).AddHours(19).AddMinutes(30);
                        }
                        else if (week == 5)
                        {
                            pre_opentime = timer.AddDays(2).AddHours(19).AddMinutes(30);
                        }

                    }
                }
                else if (lotteryQs[i].LotteryType == 2)//大乐透
                {
                    if (week == 2 || week == 4 || week == 7)//1，3，6 开奖
                    {
                        if (week == 2 || week == 7)
                        {
                            qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                            timer = DateTime.Now;
                            pre_opentime = timer.AddDays(1).AddHours(19).AddMinutes(30);
                        }
                        else if (week == 4)
                        {
                            qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                            timer = DateTime.Now;
                            pre_opentime = timer.AddDays(2).AddHours(19).AddMinutes(30);
                        }

                    }
                }
                else if (lotteryQs[i].LotteryType == 3)//排列三
                {
                    qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                    timer = DateTime.Now;
                    pre_opentime = timer.AddMinutes(19).AddMinutes(30);
                }
                else if (lotteryQs[i].LotteryType == 4)//排列五
                {
                    qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                    timer = DateTime.Now;
                    pre_opentime = timer.AddHours(19).AddMinutes(30);
                }
                else if (lotteryQs[i].LotteryType == 5)//福彩3D
                {
                    qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                    timer = DateTime.Now;
                    pre_opentime = timer.AddHours(19).AddMinutes(30);
                }
                else if (lotteryQs[i].LotteryType == 6)//七星彩
                {
                    if (week == 3 || week == 6 || week == 1)//2,5,7开奖
                    {
                        if (week == 1 || week == 6)
                        {
                            qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                            timer = DateTime.Now;
                            pre_opentime = timer.AddDays(1).AddHours(19).AddMinutes(30);
                        }
                        else if (week == 3)
                        {
                            qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                            timer = DateTime.Now;
                            pre_opentime = timer.AddDays(2).AddHours(19).AddMinutes(30);
                        }

                    }

                }
                else if (lotteryQs[i].LotteryType == 7)//七乐彩 1,3,5开奖
                {
                    if (week == 2 || week == 4 || week == 6)
                    {
                        qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                        timer = DateTime.Now;
                        pre_opentime = timer.AddDays(1).AddHours(19).AddMinutes(30);
                    }
                    else if (week == 6)
                    {
                        qs = (int.Parse(lotteryQs[i].Lotteryqs) + 1).ToString();
                        timer = DateTime.Now;
                        pre_opentime = timer.AddDays(2).AddHours(19).AddMinutes(30);
                    }
                }
                Model.LotteryQs modelQs = BLL.LotteryQs.GetModel(lotteryQs[i].id);
                modelQs.Lotteryqs = qs;
                modelQs.StartTime = timer;
                modelQs.pre_opentime = pre_opentime;
                if (timer != new DateTime())
                {
                    BLL.LotteryQs.Update(modelQs);
                }

            }
            #endregion
        }
    }


    void ComputeMediaForecaststatistics(object sender, ElapsedEventArgs e)
    {

        int inthour = e.SignalTime.Hour;
        int ihour = 03;
        int intminute = e.SignalTime.Minute;
        int iminute = 30;
        int intSecond = e.SignalTime.Second;
        int iSecond = 00;
        if (inthour == ihour && intminute == iminute && intSecond == iSecond)//凌晨3点30更新媒体预测数据
        {
            BLL.MediaForecast.UpdateForecast();//开奖后，更新媒体预测数据
        }
    }

    /// <summary>
    /// 计算星期几
    /// </summary>
    /// <param name="weekinfo"></param>
    /// <returns></returns>
    private int selectNextDate(DateTime timer)
    {
        int week = -1;
        string weekstr = timer.DayOfWeek.ToString();
        if (weekstr.Equals("Monday"))
        {
            week = 1;
        }
        else if (weekstr.Equals("Tuesday"))
        {
            week = 2;
        }
        else if (weekstr.Equals("Wednesday"))
        {
            week = 3;
        }
        else if (weekstr.Equals("Thursday"))
        {
            week = 4;
        }
        else if (weekstr.Equals("Friday"))
        {
            week = 5;
        }
        else if (weekstr.Equals("Saturday"))
        {
            week = 6;
        }
        else if (weekstr.Equals("Sunday"))
        {
            week = 7;
        }
        return week;
    }

    void Application_End(object sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码
        //下面的代码是关键，可解决IIS应用程序池自动回收的问题
        System.Threading.Thread.Sleep(1000);
        //这里设置你的web地址，可以随便指向你的任意一个aspx页面甚至不存在的页面，目的是要激发Application_Start

        string url = "http://sfrrb.com";
        System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
        System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流

    }

    void Application_Error(object sender, EventArgs e)
    {
        Exception err = Server.GetLastError();
        //在出现未处理的错误时运行的代码
        string errInfo = err.Message;
        string log = "Time:" + System.DateTime.Now.ToString() + "\r\nIP:" + Request.UserHostAddress + "\r\nUrl:";
        log += Request.Url.ToString() + "\r\nInfo:" + errInfo + "\r\nSource:" + err.Source + "\r\n";    // StackTrace:" + err.StackTrace + "\r\n";

        //清除前一个异常  

        Server.ClearError();

        //独占方式，因为文件只能由一个进程写入.
        System.IO.StreamWriter writer = null;
        try
        {
            lock (this)
            {
                // 写入日志
                string year = DateTime.Now.Year.ToString();
                string month = DateTime.Now.Month.ToString();
                string path = string.Empty;
                string filename = DateTime.Now.Day.ToString() + ".log";
                path = Server.MapPath("~/ErrorLog/") + year + "." + (month.Length < 2 ? "0" + month.ToString() : month.ToString());
                // 如果目录不存在则创建
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                System.IO.FileInfo file = new System.IO.FileInfo(path + "/" + filename);


                // 文件不存在就创建,true表示追加

                writer = new System.IO.StreamWriter(file.FullName, true);
                writer.WriteLine(log);
            }
        }
        finally
        {
            if (writer != null)
                writer.Close();

        }

        System.Web.HttpContext.Current.Response.Redirect("~/ErrorPage.aspx?Info=" + Server.UrlEncode(errInfo));

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时，才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer，则不会引发该事件。

    }

    void GetPageInfo(object sender, ElapsedEventArgs e)
    {
        int inthour = e.SignalTime.Hour;
        int ihour = 03;
        int intminute = e.SignalTime.Minute;
        int iminute = 01;
        int intSecond = e.SignalTime.Second;
        int iSecond = 00;
        if (inthour == ihour && intminute == iminute && intSecond == iSecond)//凌晨三点，获取开奖数据
        {
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/ssq.TXT", Encoding.UTF8, 1);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/dlt.TXT", Encoding.UTF8, 2);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/p3.TXT", Encoding.UTF8, 3);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/p5.TXT", Encoding.UTF8, 4);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/3d.TXT", Encoding.UTF8, 5);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/7xc.TXT", Encoding.UTF8, 6);
            BLL.LotteryAnnouncement.GetHtmlEx("http://www.17500.cn/getData/7lc.TXT", Encoding.UTF8, 7);

        }
    }


</script>
